package com.rin.controller;

import com.rin.service.DeleteService;
import com.rin.service.InsertService;
import com.rin.service.SelectService;
import com.rin.service.UpdateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author RinMi
 * @since 2023/6/15
 */

@Slf4j
public class BaseController<T> {

    @Resource
    private InsertService insertService;

    @Resource
    private DeleteService deleteService;

    @Resource
    private UpdateService updateService;

    @Resource
    private SelectService selectService;

    @PostMapping("insert")
    public void insert(@RequestBody T t) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller insert...");
            insertService.insert(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("delete")
    public void delete(T t, @RequestBody String id) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller delete...");
            deleteService.delete(t, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("deleteBatch")
    public void deleteBatch(T t, @RequestBody List<String> idList) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller deleteBatch...");
            deleteService.deleteBatch(t, idList);
        } catch (Exception e){
            e.printStackTrace();
        }
    }



    @PostMapping("update")
    public void update(@RequestBody T t, @RequestParam String id) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller update...");
            updateService.update(t, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("select")
    public Object select(T t, @RequestParam String id) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller select...");
            return selectService.select(t, id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("selectBatch")
    public List<Object> selectBatch(T t) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller selectBatch...");
            return selectService.selectBatch(t);
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @PostMapping("initRepository")
    public void initRepository(@RequestBody T t) {
        try {
            log.info("execute" + t.getClass().getSimpleName() + "Controller initRepository...");
            selectService.initRepository(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
